Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Commit

Permalink
Improving external dep logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Walter committed Jun 17, 2019
1 parent 0ff54bf commit 1acd13a
Show file tree
Hide file tree
Showing 4 changed files with 886 additions and 40 deletions.
20 changes: 6 additions & 14 deletions dist/dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,12 @@ async function dist (options) {
inlineDeps = inline.split(',');
nodeResolve = nodeResolvePlugin({ only: inlineDeps });
}
const byIsNotInlineDep = dep => inlineDeps.indexOf(dep) === -1;
const externalDeps = [...builtinModules, ...deps.filter(byIsNotInlineDep)];
const isInExternal = id => {
try {
const modulePath = require.resolve(id);
if (id !== modulePath) {
return externalDeps.some(external => modulePath.includes(external))
}
} catch (err) {
// Nothing needs to be done with this error.
}
return false
};
const external = id => externalDeps.includes(id) || isInExternal(id);
const externalModules = deps.filter(dep => inlineDeps.indexOf(dep) === -1);
const externalDeps = [...builtinModules, ...externalModules];
const external = id => (
externalDeps.includes(id) ||
externalModules.some(external => id.includes(external + path.sep))
);

// Set the default babel config.
const babelConfig = {
Expand Down
20 changes: 6 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,12 @@ export default async function dist (options) {
inlineDeps = inline.split(',')
nodeResolve = nodeResolvePlugin({ only: inlineDeps })
}
const byIsNotInlineDep = dep => inlineDeps.indexOf(dep) === -1
const externalDeps = [...builtinModules, ...deps.filter(byIsNotInlineDep)]
const isInExternal = id => {
try {
const modulePath = require.resolve(id)
if (id !== modulePath) {
return externalDeps.some(external => modulePath.includes(external))
}
} catch (err) {
// Nothing needs to be done with this error.
}
return false
}
const external = id => externalDeps.includes(id) || isInExternal(id)
const externalModules = deps.filter(dep => inlineDeps.indexOf(dep) === -1)
const externalDeps = [...builtinModules, ...externalModules]
const external = id => (
externalDeps.includes(id) ||
externalModules.some(external => id.includes(external + path.sep))
)

// Set the default babel config.
const babelConfig = {
Expand Down
91 changes: 86 additions & 5 deletions tests/snapshots/cli.tests.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ exports[`dist file generated using custom plugins 2`] = `
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var __vue_normalize__ = _interopDefault(require('vue-runtime-helpers/dist/normalize-component.js'));
//
//
//
Expand All @@ -34,6 +30,91 @@ var script = {
data: () => ({ name: 'World' })
};
function normalizeComponent(template, style, script, scopeId, isFunctionalTemplate, moduleIdentifier
/* server only */
, shadowMode, createInjector, createInjectorSSR, createInjectorShadow) {
if (typeof shadowMode !== 'boolean') {
createInjectorSSR = createInjector;
createInjector = shadowMode;
shadowMode = false;
} // Vue.extend constructor export interop.
var options = typeof script === 'function' ? script.options : script; // render functions
if (template && template.render) {
options.render = template.render;
options.staticRenderFns = template.staticRenderFns;
options._compiled = true; // functional template
if (isFunctionalTemplate) {
options.functional = true;
}
} // scopedId
if (scopeId) {
options._scopeId = scopeId;
}
var hook;
if (moduleIdentifier) {
// server build
hook = function hook(context) {
// 2.3 injection
context = context || // cached call
this.$vnode && this.$vnode.ssrContext || // stateful
this.parent && this.parent.$vnode && this.parent.$vnode.ssrContext; // functional
// 2.2 with runInNewContext: true
if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
context = __VUE_SSR_CONTEXT__;
} // inject component styles
if (style) {
style.call(this, createInjectorSSR(context));
} // register component module identifier for async chunk inference
if (context && context._registeredComponents) {
context._registeredComponents.add(moduleIdentifier);
}
}; // used by ssr in case component is cached and beforeCreate
// never gets called
options._ssrRegister = hook;
} else if (style) {
hook = shadowMode ? function () {
style.call(this, createInjectorShadow(this.$root.$options.shadowRoot));
} : function (context) {
style.call(this, createInjector(context));
};
}
if (hook) {
if (options.functional) {
// register for functional component in vue file
var originalRender = options.render;
options.render = function renderWithStyleInjection(h, context) {
hook.call(context);
return originalRender(h, context);
};
} else {
// inject component registration as beforeCreate hook
var existing = options.beforeCreate;
options.beforeCreate = existing ? [].concat(existing, hook) : [hook];
}
}
return script;
}
var normalizeComponent_1 = normalizeComponent;
/* script */
const __vue_script__ = script;
Expand All @@ -55,7 +136,7 @@ var __vue_staticRenderFns__ = [];
var Greeting = __vue_normalize__(
var Greeting = normalizeComponent_1(
{ render: __vue_render__, staticRenderFns: __vue_staticRenderFns__ },
__vue_inject_styles__,
__vue_script__,
Expand Down
Loading

0 comments on commit 1acd13a

Please sign in to comment.