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

Commit

Permalink
Fixing externalDep logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Walter committed Jun 17, 2019
1 parent 4c86bdd commit 0ff54bf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 899 deletions.
24 changes: 17 additions & 7 deletions dist/dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var path = require('path');
var path = _interopDefault(require('path'));
var readPkgUp = _interopDefault(require('read-pkg-up'));
var rollup = require('rollup');
var cjsPlugin = _interopDefault(require('rollup-plugin-commonjs'));
Expand All @@ -16,7 +16,7 @@ var hashbang = _interopDefault(require('@ianwalter/rollup-plugin-hashbang'));

async function dist (options) {
// Read modules package.json.
const { package: pkg, path: path$1 } = await readPkgUp();
const { package: pkg, path: projectPath } = await readPkgUp();

// TODO: comment
const hasFormat = options.cjs || options.esm || options.browser;
Expand All @@ -25,8 +25,9 @@ async function dist (options) {
// Deconstruct options and set defaults if necessary.
let {
name = options.name || npmShortName(pkg.name),
input = options.input || path.resolve(path.join(path.dirname(path$1), 'index.js')),
output = options.output || path.join(path.dirname(path$1), 'dist'),
input = options.input ||
path.resolve(path.join(path.dirname(projectPath), 'index.js')),
output = options.output || path.join(path.dirname(projectPath), 'dist'),
cjs = getFormat(options.cjs, pkg.main),
esm = getFormat(options.esm, pkg.module),
browser = getFormat(options.browser, pkg.browser)
Expand Down Expand Up @@ -61,9 +62,18 @@ async function dist (options) {
}
const byIsNotInlineDep = dep => inlineDeps.indexOf(dep) === -1;
const externalDeps = [...builtinModules, ...deps.filter(byIsNotInlineDep)];
const external = id => (
externalDeps.includes(id) || externalDeps.some(n => id.includes(n + '/'))
);
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);

// Set the default babel config.
const babelConfig = {
Expand Down
46 changes: 28 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirname, join, resolve, extname } from 'path'
import path from 'path'
import readPkgUp from 'read-pkg-up'
import { rollup } from 'rollup'
import cjsPlugin from 'rollup-plugin-commonjs'
Expand All @@ -12,7 +12,7 @@ import hashbang from '@ianwalter/rollup-plugin-hashbang'

export default async function dist (options) {
// Read modules package.json.
const { package: pkg, path } = await readPkgUp()
const { package: pkg, path: projectPath } = await readPkgUp()

// TODO: comment
const hasFormat = options.cjs || options.esm || options.browser
Expand All @@ -21,8 +21,9 @@ export default async function dist (options) {
// Deconstruct options and set defaults if necessary.
let {
name = options.name || npmShortName(pkg.name),
input = options.input || resolve(join(dirname(path), 'index.js')),
output = options.output || join(dirname(path), 'dist'),
input = options.input ||
path.resolve(path.join(path.dirname(projectPath), 'index.js')),
output = options.output || path.join(path.dirname(projectPath), 'dist'),
cjs = getFormat(options.cjs, pkg.main),
esm = getFormat(options.esm, pkg.module),
browser = getFormat(options.browser, pkg.browser)
Expand All @@ -36,7 +37,7 @@ export default async function dist (options) {
// Import plugins file if specified.
let plugins = []
if (typeof options.plugins === 'string') {
const input = resolve(options.plugins)
const input = path.resolve(options.plugins)
const external = Object.keys(pkg.devDependencies || {})
const { generate } = await rollup({ input, external })
const { output: [{ code }] } = await generate({ format: 'cjs' })
Expand All @@ -57,9 +58,18 @@ export default async function dist (options) {
}
const byIsNotInlineDep = dep => inlineDeps.indexOf(dep) === -1
const externalDeps = [...builtinModules, ...deps.filter(byIsNotInlineDep)]
const external = id => (
externalDeps.includes(id) || externalDeps.some(n => id.includes(n + '/'))
)
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)

// Set the default babel config.
const babelConfig = {
Expand Down Expand Up @@ -104,16 +114,16 @@ export default async function dist (options) {
let esmCode = (esm || browser) ? esmBundle.output[0].code : undefined

// Determine the output file paths.
const dir = extname(output) ? dirname(output) : output
const cjsPath = typeof cjs === 'string' && extname(cjs)
? resolve(cjs)
: join(dir, `${name}.js`)
const esmPath = typeof esm === 'string' && extname(esm)
? resolve(esm)
: join(dir, `${name}.m.js`)
const browserPath = typeof browser === 'string' && extname(browser)
? resolve(browser)
: join(dir, `${name}.browser.js`)
const dir = path.extname(output) ? path.dirname(output) : output
const cjsPath = typeof cjs === 'string' && path.extname(cjs)
? path.resolve(cjs)
: path.join(dir, `${name}.js`)
const esmPath = typeof esm === 'string' && path.extname(esm)
? path.resolve(esm)
: path.join(dir, `${name}.m.js`)
const browserPath = typeof browser === 'string' && path.extname(browser)
? path.resolve(browser)
: path.join(dir, `${name}.browser.js`)

// Return an object with the properties that use the file path as the key and
// the source code as the value.
Expand Down
91 changes: 5 additions & 86 deletions tests/snapshots/cli.tests.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ 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 @@ -30,91 +34,6 @@ 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 @@ -136,7 +55,7 @@ var __vue_staticRenderFns__ = [];
var Greeting = normalizeComponent_1(
var Greeting = __vue_normalize__(
{ render: __vue_render__, staticRenderFns: __vue_staticRenderFns__ },
__vue_inject_styles__,
__vue_script__,
Expand Down
Loading

0 comments on commit 0ff54bf

Please sign in to comment.