Skip to content

Commit

Permalink
feat(loadOption): Nested chunknames as major release
Browse files Browse the repository at this point in the history
Reverted this from 1.5.2 in order to re-introduce as a breaking change with a major release version bump. I also added more conventional looking tests

BREAKING CHANGE: Chunknames no longer output directories. If the chunkname was components/component,then the output would be a directory called components with the component inside it. Keeping with webpack 3 chunking conventions, chunk folders are represented in the filename with a hyphen as a seperator: components-component.js. the side affect is flattened build outputs

re #45
  • Loading branch information
ScriptedAlchemy committed May 8, 2018
1 parent 0f77ffd commit c1bb186
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ coverage
node_modules
*.log
.idea
.DS_Store
58 changes: 58 additions & 0 deletions __tests__/__snapshots__/index.js.snap
Expand Up @@ -233,6 +233,64 @@ _universalImport({
"
`;
exports[`static import (import as function with relative paths + nested folder) 1`] = `
"
const obj = {component:()=>import(\`../components/nestedComponent\`)}; ()=> obj.component()
↓ ↓ ↓ ↓ ↓ ↓
var _path = _interopRequireDefault(require(\\"path\\")).default;
var _importCss = _interopRequireDefault(require(\\"babel-plugin-universal-import/importCss\\")).default;
var _universalImport = _interopRequireDefault(require(\\"babel-plugin-universal-import/universalImport\\")).default;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const obj = {
component: () => _universalImport({
id: \\"../components/nestedComponent\\",
file: \\"currentFile.js\\",
load: () => Promise.all([import(
/* webpackChunkName: 'components-nestedComponent' */
\`../components/nestedComponent\`), _importCss(\\"components/nestedComponent\\", {})]).then(proms => proms[0]),
path: () => _path.join(__dirname, \`../components/nestedComponent\`),
resolve: () => require.resolveWeak(\`../components/nestedComponent\`),
chunkName: () => \\"components-nestedComponent\\"
})
};
() => obj.component();
"
`;
exports[`static import (relative paths + nested folder) 1`] = `
"
import(\`../components/nestedComponent\`)
↓ ↓ ↓ ↓ ↓ ↓
var _path = _interopRequireDefault(require(\\"path\\")).default;
var _importCss = _interopRequireDefault(require(\\"babel-plugin-universal-import/importCss\\")).default;
var _universalImport = _interopRequireDefault(require(\\"babel-plugin-universal-import/universalImport\\")).default;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
_universalImport({
id: \\"../components/nestedComponent\\",
file: \\"currentFile.js\\",
load: () => Promise.all([import(
/* webpackChunkName: 'components-nestedComponent' */
\`../components/nestedComponent\`), _importCss(\\"components/nestedComponent\\", {})]).then(proms => proms[0]),
path: () => _path.join(__dirname, \`../components/nestedComponent\`),
resolve: () => require.resolveWeak(\`../components/nestedComponent\`),
chunkName: () => \\"components-nestedComponent\\"
});
"
`;
exports[`static import (string template + relative paths) 1`] = `
"
import(\`../../base\`)
Expand Down
4 changes: 4 additions & 0 deletions __tests__/index.js
Expand Up @@ -25,6 +25,10 @@ pluginTester({
'static import (with file extension)': 'import("./Foo.js")',
'static import (string template)': 'import(`./base`)',
'static import (string template + relative paths)': 'import(`../../base`)',
'static import (import as function with relative paths + nested folder)':
'const obj = {component:()=>import(`../components/nestedComponent`)}; ()=> obj.component()',
'static import (relative paths + nested folder)':
'import(`../components/nestedComponent`)',
'dynamic import (string template)': 'import(`./base/${page}`)',
'dynamic import (string template with nested folder)':
'import(`./base/${page}/nested/folder`)',
Expand Down
12 changes: 12 additions & 0 deletions index.js
Expand Up @@ -197,6 +197,14 @@ function chunkNameOption(t, chunkNameTemplate, importArgNode) {
return t.objectProperty(t.identifier('chunkName'), chunkName)
}

function checkForNestedChunkName(node) {
const generatedChunkName = getMagicCommentChunkName(node)
const isNested =
generatedChunkName.indexOf('[request]') === -1 &&
generatedChunkName.indexOf('/') > -1
return isNested && prepareChunkNamePath(generatedChunkName)
}

module.exports = function universalImportPlugin({ types: t, template }) {
const chunkNameTemplate = template('() => MODULE')
const pathTemplate = template('() => PATH.join(__dirname, MODULE)')
Expand All @@ -214,6 +222,10 @@ module.exports = function universalImportPlugin({ types: t, template }) {

const importArgNode = getImportArgPath(p).node
t.existingChunkName = existingMagicCommentChunkName(importArgNode)
// no existing chunkname, no problem - we will reuse that for fixing nested chunk names
if (!t.existingChunkName) {
t.existingChunkName = checkForNestedChunkName(importArgNode)
}
const universalImport = getImport(p, IMPORT_UNIVERSAL_DEFAULT)

const cssOptions = {
Expand Down

0 comments on commit c1bb186

Please sign in to comment.