From ab6182d392230bc4d9dc9a3a17e754f5df3832a0 Mon Sep 17 00:00:00 2001 From: Luke Opperman Date: Wed, 5 Dec 2018 11:12:11 -0500 Subject: [PATCH 1/5] Write script bundles to sub-paths to avoid basename conflicts. Fix tests for component output and catch missing reference data. --- src/index.js | 29 +++- src/test/apps/other/one.js | 7 + src/test/data/component/component-one.html.js | 161 +----------------- .../data/component/component-one.html.js.map | 1 + src/test/data/script/other-one.js | 2 + src/test/data/script/other-one.js.map | 1 + src/test/index.js | 34 ++-- 7 files changed, 59 insertions(+), 176 deletions(-) create mode 100644 src/test/apps/other/one.js create mode 100644 src/test/data/component/component-one.html.js.map create mode 100644 src/test/data/script/other-one.js create mode 100644 src/test/data/script/other-one.js.map diff --git a/src/index.js b/src/index.js index 9ebdf98..c7dfeb0 100755 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,7 @@ import webpack from 'webpack' import UglifyJsPlugin from 'uglifyjs-webpack-plugin' import path from 'path' import fs from 'fs-extra' -import _ from 'underscore' +import {omit, isString, isEmpty, zip} from 'underscore' import morph from 'morph' import traverse from 'traverse' @@ -14,6 +14,24 @@ import {templater} from 'nxus-templater' import {application as app, NxusModule} from 'nxus-core' +function subPath(script) { + // determine subpath of script for output + script = path.normalize(script) + let paths = zip(script.split(path.sep), __dirname.split(path.sep)) + let a, b, c, scriptPaths = [] + while (c = paths.shift()) { + [a,b] = c + if (scriptPaths == '' && a == b) { + continue + } + if (a) { + scriptPaths.push(a) + } + } + return scriptPaths.join(path.sep) +} + + /** * * [![Build Status](https://travis-ci.org/nxus/clientjs.svg?branch=master)](https://travis-ci.org/nxus/clientjs) @@ -109,8 +127,8 @@ class ClientJS extends NxusModule { super() this._outputPaths = {} - if(_.isEmpty(this.config.babel)) - this.config.babel = _.omit(require('rc')('babel', {}, {}), '_', 'config', 'configs') + if(isEmpty(this.config.babel)) + this.config.babel = omit(require('rc')('babel', {}, {}), '_', 'config', 'configs') this.config.babel.cacheDirectory = true this._fromConfigBundles(app) @@ -169,8 +187,7 @@ class ClientJS extends NxusModule { * @param {[type]} script the path of the script file to include */ includeScript(templateName, script) { - let scriptName = path.basename(script) - let outputPath = scriptName + let outputPath = subPath(script) let imports = [], headScripts = [] @@ -292,7 +309,7 @@ class ClientJS extends NxusModule { let localConfig = Object.assign({}, this.config.webpackConfig) // hydrate regexps from json config traverse(localConfig).forEach(function(n) { - if (_.isString(n) && n.substring(0,1) == "/" && n.substring(-1, 1) == "/") { + if (isString(n) && n.substring(0,1) == "/" && n.substring(-1, 1) == "/") { this.update(new RegExp(n.substring(1, n.length-1))) } }) diff --git a/src/test/apps/other/one.js b/src/test/apps/other/one.js new file mode 100644 index 0000000..715822e --- /dev/null +++ b/src/test/apps/other/one.js @@ -0,0 +1,7 @@ +import two from '../two' + +let test = 'one'; + +export default function (n) { + return n * two(n); +}; diff --git a/src/test/data/component/component-one.html.js b/src/test/data/component/component-one.html.js index 4633c74..4aa424e 100644 --- a/src/test/data/component/component-one.html.js +++ b/src/test/data/component/component-one.html.js @@ -1,159 +1,2 @@ -/******/ (function(modules) { // webpackBootstrap -/******/ // The module cache -/******/ var installedModules = {}; -/******/ -/******/ // The require function -/******/ function __webpack_require__(moduleId) { -/******/ -/******/ // Check if module is in cache -/******/ if(installedModules[moduleId]) { -/******/ return installedModules[moduleId].exports; -/******/ } -/******/ // Create a new module (and put it into the cache) -/******/ var module = installedModules[moduleId] = { -/******/ i: moduleId, -/******/ l: false, -/******/ exports: {} -/******/ }; -/******/ -/******/ // Execute the module function -/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); -/******/ -/******/ // Flag the module as loaded -/******/ module.l = true; -/******/ -/******/ // Return the exports of the module -/******/ return module.exports; -/******/ } -/******/ -/******/ -/******/ // expose the modules object (__webpack_modules__) -/******/ __webpack_require__.m = modules; -/******/ -/******/ // expose the module cache -/******/ __webpack_require__.c = installedModules; -/******/ -/******/ // define getter function for harmony exports -/******/ __webpack_require__.d = function(exports, name, getter) { -/******/ if(!__webpack_require__.o(exports, name)) { -/******/ Object.defineProperty(exports, name, { -/******/ configurable: false, -/******/ enumerable: true, -/******/ get: getter -/******/ }); -/******/ } -/******/ }; -/******/ -/******/ // getDefaultExport function for compatibility with non-harmony modules -/******/ __webpack_require__.n = function(module) { -/******/ var getter = module && module.__esModule ? -/******/ function getDefault() { return module['default']; } : -/******/ function getModuleExports() { return module; }; -/******/ __webpack_require__.d(getter, 'a', getter); -/******/ return getter; -/******/ }; -/******/ -/******/ // Object.prototype.hasOwnProperty.call -/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; -/******/ -/******/ // __webpack_public_path__ -/******/ __webpack_require__.p = ""; -/******/ -/******/ // Load entry module and return exports -/******/ return __webpack_require__(__webpack_require__.s = 0); -/******/ }) -/************************************************************************/ -/******/ ([ -/* 0 */ -/***/ (function(module, exports, __webpack_require__) { - -"use strict"; - - -var RegisterHtmlTemplate = __webpack_require__(1); - -RegisterHtmlTemplate.register(" "); - -Polymer({ - is: 'icon-toggle', - properties: { - pressed: { - type: Boolean, - notify: true, - reflectToAttribute: true, - value: false - }, - toggleIcon: { - type: String - } - }, - listeners: { - "tap": "toggle" - }, - toggle: function toggle() { - this.pressed = !this.pressed; - } -}); - -/***/ }), -/* 1 */ -/***/ (function(module, exports) { - -/* eslint-env browser */ - -class RegisterHtmlTemplate { - /** - * Create a `