This repository has been archived by the owner on May 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix assetUrl for non-browser compilations, simplify implementation
- Loading branch information
Showing
19 changed files
with
82 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
build/babel-plugins/babel-plugin-asseturl/test/fixtures/expected-import-destructuring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { assetUrl } from 'fusion-core'; | ||
import { assetUrl as assetUrlOther } from 'assetUrl'; | ||
const path = './test'; | ||
assetUrl(require("__SECRET_FILE_LOADER__?storeFile=true&storeFileTarget=node!./path")); | ||
assetUrl(require("__SECRET_FILE_LOADER__!./path")); | ||
assetUrlOther(path); | ||
assetUrl(require("__SECRET_FILE_LOADER__?storeFile=true&storeFileTarget=node!./path")); | ||
assetUrl(require("__SECRET_FILE_LOADER__!./path")); |
2 changes: 1 addition & 1 deletion
2
build/babel-plugins/babel-plugin-asseturl/test/fixtures/expected-import-destructuring-as
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { assetUrl as frameworkAssetUrl } from 'fusion-core'; | ||
import assetUrl from 'assetURL'; | ||
assetUrl('./path'); | ||
frameworkAssetUrl(require("__SECRET_FILE_LOADER__?storeFile=true&storeFileTarget=node!./path")); | ||
frameworkAssetUrl(require("__SECRET_FILE_LOADER__!./path")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,25 @@ | ||
/** Copyright (c) 2018 Uber Technologies, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
/* eslint-env node */ | ||
// @flow | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Tobias Koppers @sokra | ||
*/ | ||
const path = require('path'); | ||
const loaderUtils = require('loader-utils'); | ||
const storageSingleton = require('./file-loader-asset-storage.js'); | ||
|
||
module.exports = function fileLoader(content /*: string */) { | ||
if (!this.emitFile) | ||
throw new Error('emitFile is required from module system'); | ||
|
||
const options = loaderUtils.getOptions(this) || {}; | ||
|
||
const config = { | ||
publicPath: undefined, | ||
useRelativePath: false, | ||
name: '[hash].[ext]', | ||
storeFile: false, | ||
storeFileTarget: null, | ||
}; | ||
|
||
// options takes precedence over config | ||
Object.keys(options).forEach(attr => { | ||
config[attr] = options[attr]; | ||
}); | ||
const path = require("path"); | ||
const loaderUtils = require("loader-utils"); | ||
|
||
const context = | ||
options.context || | ||
this.rootContext || | ||
(this.options && this.options.context); | ||
let url = loaderUtils.interpolateName(this, config.name, { | ||
context, | ||
content, | ||
regExp: void 0, | ||
module.exports = function fileLoader(content /*: string */) { | ||
const url = loaderUtils.interpolateName(this, "[hash].[ext]", { | ||
context: this.rootContext, | ||
content | ||
}); | ||
|
||
let outputPath = ''; | ||
|
||
const filePath = this.resourcePath; | ||
if (config.useRelativePath) { | ||
// eslint-disable-next-line no-mixed-operators | ||
const issuerContext = | ||
(this._module && this._module.issuer && this._module.issuer.context) || | ||
context; | ||
const relativeUrl = | ||
issuerContext && | ||
path | ||
.relative(issuerContext, filePath) | ||
.split(path.sep) | ||
.join('/'); | ||
const relativePath = relativeUrl && `${path.dirname(relativeUrl)}/`; | ||
// eslint-disable-next-line no-bitwise | ||
if (~relativePath.indexOf('../')) { | ||
// eslint-disable-line no-bitwise | ||
outputPath = path.posix.join(outputPath, relativePath, url); | ||
} else { | ||
outputPath = relativePath + url; | ||
} | ||
url = relativePath + url; | ||
} else { | ||
outputPath = url; | ||
} | ||
// Assets should always go into client dist directory, regardless of source | ||
const outputPath = path.posix.join("../client", url); | ||
|
||
let publicPath = `__webpack_public_path__ + ${JSON.stringify(url)}`; | ||
if (config.publicPath !== undefined) { | ||
// support functions as publicPath to generate them dynamically | ||
publicPath = JSON.stringify( | ||
typeof config.publicPath === 'function' | ||
? config.publicPath(url) | ||
: config.publicPath + url | ||
); | ||
} | ||
this.emitFile(outputPath, content); | ||
|
||
const storage = storageSingleton.getStorage(); | ||
const {storeFile, storeFileTarget} = config; | ||
if (options.emitFile === undefined || options.emitFile) { | ||
// when storeFile param is passed we don't emit a file | ||
// but store it to be added added later as an additional asset to a compilation | ||
// it allows adding these files in a different compilation | ||
if (storeFile && (!storeFileTarget || this.target === storeFileTarget)) { | ||
storage.addFile(outputPath, content); | ||
} else { | ||
this.emitFile(outputPath, content); | ||
} | ||
} | ||
|
||
return `module.exports = ${publicPath};`; | ||
return `module.exports = __webpack_public_path__ + ${JSON.stringify(url)};`; | ||
}; | ||
|
||
module.exports.raw = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
assumeNoImportSideEffects: true | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test server assets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
assumeNoImportSideEffects: true | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
server asset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
universal asset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import App, {assetUrl} from 'fusion-core'; | ||
|
||
import {serverAsset} from "./server-assets.js"; | ||
|
||
const asset = assetUrl('./assets/universal-asset.txt'); | ||
|
||
export default async function() { | ||
const app = new App('element', el => el); | ||
if (__NODE__) { | ||
serverAsset(); | ||
} | ||
console.log(asset); | ||
return app; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {assetUrl} from 'fusion-core'; | ||
|
||
export const serverAsset = () => assetUrl('./assets/server-asset.txt'); |